import {round, add, subtract, multiply} from 'mathjs'

export const getSumkey=(data, key) => {
  if(data.length ===1){
    return data[0][key]
  }else if(data.length ===0){
    return 0
  }else{
    return data.reduce((prev, cur, i) => {
      // console.log(prev, cur, i,"ss")
      if(i===1){
        return round(add(!prev[key]?0:prev[key], !cur[key]?0:cur[key]), 3) 
      }else{
        return round(add(!prev?0:prev, !cur[key]?0:cur[key]), 3)
      }   
    })
  }
    
};
export const getSum=(data) => {
  return getSumkey(data, "value")
};
export const getSumm=(data) => {
  if(data.length ===1){
    return data[0]
  }else if(data.length ===0){
    return 0
  }else{
    return data.reduce((prev, cur, i) => {
      if(i===1){
        return round(add(!prev?0:prev, !cur?0:cur), 3) 
      }else{
        return round(add(!prev?0:prev, !cur?0:cur), 3)
      }   
    })
  }
};
export const getqucost=(a, b, c, d) => {
  //材料成本：材料单价×毛重-废料重量×废料单价
  return round(subtract(multiply(!a?0:a, !b?0:b), multiply(!c?0:c, !d?0:d)), 3)
}

export const getadd=(a, b) => {
//材料成本：材料单价×毛重-废料重量×废料单价
  return round(add(!a?0:a, !b?0:b), 3)
}

export const getkG=(arr) => {
  let newarr= arr.filter((i) => {
    // return /^[K,k][G,g]$/.test(i.unit)
    return i.unit===10
  })
  return getSumkey(newarr, "materialCost")
}//KG之和

export const getminus=(sum, arr) => {
//合计减*
  return round(subtract(!sum?0:sum, getSumm(arr)), 3)
}

export const formItemLayoutint={
  labelCol: {
    xs: { span: 24 },
    sm: { span: 4 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 20 },
  },
}
export const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 8 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 16 },
  },
} //常规表单布局
export const formItemLayoutinput = {
  labelCol: {
    xs: { span: 0 },
    sm: { span: 0 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 24 },
  },
}
